home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / help / VARIABLES < prev    next >
Text File  |  1994-04-25  |  4KB  |  98 lines

  1. VARIABLES:
  2.  
  3.     General discussion of variables, including local, static, and
  4.     global scoping rules. 
  5.  
  6.     Variable names must start with a letter, but can contain
  7.     numbers, and underscores ( `_' ) after the 1st character. The
  8.     binary and unary arithmetic operators are not allowed in
  9.     variable names. RLaB is case sensitive. The variables `A' and
  10.     `a' are different.  There is no restriction on the length of
  11.     variables names. Variable names that start with a `_' will not
  12.     show up when who(), or what() are executed, although they do
  13.     exist, and can be used like another variable.
  14.  
  15.     A variable can hold any kind of data. Variables hold scalar
  16.     and matrix numerics as well as scalar and matrix strings,
  17.     lists, built-in functions, and user-functions.
  18.  
  19.     A variable must be initialized, or used on the left-hand-side
  20.     of an assignment before it can be otherwise used. Trying to
  21.     use an uninitialized variable will result in an `UNDEFINED'
  22.     message.
  23.  
  24.     Typing `who()' produces a list of the active variables in the
  25.     global workspace.  This list includes everything except
  26.     builtin and user functions. Typing `what()' will produce a
  27.     list of all the active builtin and user functions in the
  28.     global workspace.
  29.  
  30.     Assignment to a variable destroys the current contents, and
  31.     replaces them with the contents of the right hand side
  32.     expression.  At present builtin functions cannot be
  33.     re-assigned, but user-functions (and any other variable) can
  34.     be re-assigned at any time.
  35.  
  36.     The storage space that a variable's data occupies can be
  37.     freed for other usage with the clear command (see `help
  38.     clear').
  39.  
  40.     -------------------------------------------------------------
  41.  
  42.     There are three scopes that a variable can exist in: local,
  43.     file-static, and global.
  44.  
  45.     Global variables are visible to all functions and files, at
  46.     all times. Global variable scope is the default in RLaB. This
  47.     means that you can access global variables within functions
  48.     without special declarations or statements.
  49.  
  50.     Local variables can only exist within functions, and are
  51.     declared with the `local' statement. Since global scope is
  52.     always the default, local declarations must be made for all
  53.     variables that need to be "hidden" from the global
  54.     environment. Local variables are created each time the
  55.     function is called, and are destroyed each time execution
  56.     leaves the function (unless the local variable is returned to
  57.     the calling environment). Local variables are UNDEFINED upon
  58.     creation - the function must initialize each local variable
  59.     before usage.
  60.  
  61.     File-static variables are visible only to other functions, and
  62.     statements within the file they are declared in. the static
  63.     statement creates variables that will only be seen by other
  64.     statements (including functions) within the same file.
  65.     File-static variables are created once, and are not destroyed
  66.     until the RLaB session is terminated. We will illustrate with
  67.     a simple example:
  68.  
  69.         //-------- file count.r -----------------------
  70.  
  71.         static (cnt)
  72.         cnt = 0;
  73.  
  74.         //
  75.         // Count the number of times something happens
  76.         //
  77.  
  78.         count = function ( N )
  79.         {
  80.           cnt = cnt + N;
  81.           return cnt;
  82.         };
  83.  
  84.         //--------------------------------------------
  85.     
  86.     In this example the file count.r contains a static variable
  87.     and a user-function. The user function simply maintains a sum
  88.     of the arguments that count() is called with. During a
  89.     particular RLaB session count() will keep track of the sum of
  90.     all the values of N. cnt will not appear in the
  91.     global-symbol-table. Note that any variable can be file-static
  92.     - thus, we can "hide" functions in files by declaring them
  93.     static (remember functions are variables).
  94.  
  95.     For a more elaborate example study the file misc/plplot.r in
  96.     the original RLaB source distribution. Plplot.r makes
  97.     extensive use of file-static variables.
  98.